package cn.anecansaitin.whimsy.common.extension.comboSystem.skills.rockSceptre;

import cn.anecansaitin.whimsy.common.capabilities.IManaStorage;
import cn.anecansaitin.whimsy.common.capabilities.RockScepterManaStorage;
import cn.anecansaitin.whimsy.common.entity.StoneSpike;
import cn.anecansaitin.whimsy.common.extension.comboSystem.ExtraData;
import cn.anecansaitin.whimsy.common.extension.comboSystem.INormalSkill;
import cn.anecansaitin.whimsy.common.item.RockSceptre;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib.animatable.GeoItem;

public class Attack7 implements INormalSkill {
    @Override
    public int precastTick() {
        return 0;
    }

    @Override
    public int activeTick() {
        return 1;
    }

    @Override
    public int backswingTick() {
        return 10;
    }

    @Override
    public void prePrecast(Player player, ExtraData extra) {

    }

    @Override
    public void precast(int skillTick, Player player, ExtraData extra) {

    }

    @Override
    public void preActive(Player player, ExtraData extra) {
        if (!player.level().isClientSide) {
            extra.getData("itemStack", ItemStack.class).ifPresent((i) -> {
                RockSceptre item = (RockSceptre) i.getItem();
                item.triggerAnim(player, GeoItem.getOrAssignId(i, (ServerLevel) player.level()), "con", "attack7");
                item.triggerAnim(player, GeoItem.getOrAssignId(i, (ServerLevel) player.level()), "stone", "stone_spike");
            });

            player.level().playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.EVOKER_CAST_SPELL, player.getSoundSource(), 1, 1 + player.getRandom().nextFloat());
        }
    }

    @Override
    public void active(int skillTick, Player player, ExtraData extra) {
        if (!player.onGround()) {
            return;
        }

        extra.getData("itemStack", ItemStack.class).ifPresent(i -> {
            IManaStorage manaStorage = i.getCapability(IManaStorage.MANA_STORAGE);

            if (manaStorage == null) {
                return;
            }

            if (manaStorage.getManaStored(RockScepterManaStorage.DIORITE_TYPE) < 150) {
                return;
            }

            manaStorage.extractMana(RockScepterManaStorage.DIORITE_TYPE, 150, false);
            StoneSpike.summonAtLine(player, player.getX(), player.getY(), player.getZ(), player.getYRot(), 5);
            StoneSpike.summonAtLine(player, player.getX(), player.getY(), player.getZ(), player.getYRot() - 20, 5);
            StoneSpike.summonAtLine(player, player.getX(), player.getY(), player.getZ(), player.getYRot() + 20, 5);
        });
    }

    @Override
    public void preBackswing(Player player, ExtraData extra) {

    }

    @Override
    public void backswing(int skillTick, Player player, ExtraData extra) {

    }

    @Override
    public void finish(Player player, ExtraData extra) {
        if (!player.level().isClientSide) {
            extra.getData("itemStack", ItemStack.class).ifPresent((i) -> {
                RockSceptre item = (RockSceptre) i.getItem();
                item.triggerAnim(player, GeoItem.getOrAssignId(i, (ServerLevel) player.level()), "con", "idle");
                item.triggerAnim(player, GeoItem.getOrAssignId(i, (ServerLevel) player.level()), "stone", "idle");
            });
        }
    }

    @Override
    public boolean canCachingKey(int skillTick, ExtraData extra) {
        return false;
    }

    @Override
    public boolean canDeriving(int skillTick, ExtraData extra) {
        return false;
    }

    @Override
    public String getName() {
        return "attack7";
    }
}
